package com.telecomyt.jwportal.activity.contacts;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.telecomyt.jwportal.R;
import com.telecomyt.jwportal.activity.contacts.adapter.MyAdapter;
import com.telecomyt.jwportal.activity.contacts.bean.RegionInfo;
import com.telecomyt.jwportal.activity.contacts.utils.PhoneUtil;
import com.telecomyt.jwportal.activity.contacts.view.LetterSideBarView;
import com.telecomyt.jwportal.activity.contacts.view.SideBarTouchListener;
import com.telecomyt.jwportal.base.BaseActivity;
import com.telecomyt.jwportal.base.BasePresenter;
import com.telecomyt.jwportal.utils.ToastUtils;
import com.telecomyt.jwportal.widget.FraToolBar;

import java.util.Collections;
import java.util.List;

import butterknife.BindView;

/**
 * @date: 2020/10/22
 * @author: chengDa
 * @description: 手机通讯录
 */
public class ContactsListActivity extends BaseActivity {


    @BindView(R.id.mFraToolBar)
    FraToolBar mFraToolBar;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.mLetterView)
    LetterSideBarView mLetterView;
    @BindView(R.id.mTextView)
    TextView mTextView;
    private Handler handler;
    private LinearLayoutManager mLinearLayoutManager;
    private List<RegionInfo> phone;
    private MyAdapter myAdapter;

    public static void start(Context context) {
        Intent starter = new Intent(context, ContactsListActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_contacts_list;
    }

    @Override
    protected void initView() {
        mFraToolBar.setLeftFinish(this);
        PhoneUtil phoneUtil = new PhoneUtil(this);
        phone = phoneUtil.getPhone();
        Collections.sort(phone, (lhs, rhs) -> lhs.getCode().compareTo(rhs.getCode()));
        mLinearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        myAdapter = new MyAdapter(this, phone);
        mRecyclerView.setAdapter(myAdapter);
        myAdapter.setOnRecyclerViewListener(new MyAdapter.MyItemClickListener() {
            @Override
            public void itemClick(int position) {
                ToastUtils.showShort("点击了" + position);
            }

            @Override
            public void itemClickCall(int position, String phone) {
                ToastUtils.showShort("点击了" + position + "电话" + phone);

            }
        });
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                mTextView.setVisibility(View.GONE);
            }
        };

        mLetterView.setOnSideBarTouchListener(new SideBarTouchListener() {
            @Override
            public void onTouch(String letter, boolean isTouch) {
                mTextView.setVisibility(View.VISIBLE);
                mTextView.setText(letter);
                handler.sendEmptyMessageDelayed(0, 3000);
                updateView(letter);
            }
        });
    }

    private void updateView(String word) {
        for (int i = 0; i < phone.size(); i++) {
            RegionInfo regionInfo = phone.get(i);
            String code = regionInfo.getCode().substring(0, 1);
            if (word.equals(code)) {
                /**
                 * 准确定位到指定位置，并且将指定位置的item置顶，
                 * 若直接调用scrollToPosition(...)方法，则不会置顶。
                 **/
                mLinearLayoutManager.scrollToPositionWithOffset(i, 0);
                //从下向上填充
//                mLinearLayoutManager.setStackFromEnd(true);
                break;
            }
        }
    }


    @Override
    protected void initData() {

    }

    @Override
    protected BasePresenter creatPresenter() {
        return null;
    }


}